package weaver.interfaces.suntai.action;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.suntai.utils.InvokeHelper;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-05-12
 * @Description: 采购合同申请流程
 * @ModifyLog:
 **/

public class WeaProceedsContractAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String resultText = "";
        RecordSet rs = new RecordSet();
        writeLog( ">>>>>>WeaProceedsContractAction>>>>>" + requestid );
        try {
            rs.executeQuery( "select * from " + tablename + " where requestid=?", requestid );
            rs.next();
            String mainid = Util.null2String( rs.getString( "id" ) );
            String qybh = Util.null2String( rs.getString( "qybh" ) );
            String htbhwb = Util.null2String( rs.getString( "htbhwb" ) );
            String zhbm = Util.null2String( rs.getString( "zhbm" ) );
            String cjr = Util.null2String( rs.getString( "cjr" ) );
            int counts  =0;
            String msg = "";
            rs.executeQuery( "select * from " + tablename + "_dt1 where mainid=? and zt=0", mainid );
            while (rs.next()) {
                String id = Util.null2String( rs.getString( "id" ) );
                JSONObject jsonData = new JSONObject();
                //企业编号
                jsonData.put( "companyCode", qybh );
                //合同编号
                jsonData.put( "contractCode", htbhwb );
                //收款方式
                jsonData.put( "proceedsWay", Util.null2String( rs.getString( "skfsd" ) ) );
                //收入日期
                jsonData.put( "proceedsDate", Util.null2String( rs.getString( "srrq" ) ) );
                //收款金额
                jsonData.put( "proceedsAmount", Util.null2String( rs.getString( "skje" ) ) );
                //收款比例
                jsonData.put( "proceedsRatio", Util.null2String( rs.getString( "skbl" ) ) );
                //财务收款凭证号
                jsonData.put( "voucherNum", Util.null2String( rs.getString( "cwskpzh" ) ) );
                //收款币种
                jsonData.put( "currency", Util.null2String( rs.getString( "skbzd" ) ) );
                //对方账户
                jsonData.put( "oppAcctNo", Util.null2String( rs.getString( "dfzh" ) ) );
                //对方户名
                jsonData.put( "oppAcctName", Util.null2String( rs.getString( "dfhm" ) ) );
                //对方开户行
                jsonData.put( "oppBranch", Util.null2String( rs.getString( "dfkhx" ) ) );
                //创建人
                jsonData.put( "createBy", cjr );
                //租户编码
                jsonData.put( "tenantCode", zhbm );
                //状态
                jsonData.put( "status", "rkz" );
                writeLog( ">>>>>>jsonData>>>>>" + jsonData.toJSONString());
                resultText = InvokeHelper.addProceedscont( jsonData.toJSONString() );
                writeLog( ">>>>>>resultText>>>>>" + resultText);
                JSONObject resultJson = JSONObject.parseObject( resultText );
                String code = Util.null2String( resultJson.getString( "code" ) );
                String message = Util.null2String( resultJson.getString( "message" ) );
                if ("200".equals( code )) {
                    rs.executeUpdate( "update " + tablename + "_dt1 set zt=1 where mainid=? and id=?", mainid, id );
                }else {
                    counts++;
                    msg = message;
                }
            }
            if (counts>0) {
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "接口返回异常信息：" + msg );
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }

    }
}
